package com.example.himalaya.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.himalaya.DetailActivity;
import com.example.himalaya.R;
import com.example.himalaya.adapters.RecommendListAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.interfaces.IRecommendViewCallBack;
import com.example.himalaya.presenters.AlbumDetailPresenter;
import com.example.himalaya.presenters.RecommendPresenter;
import com.example.himalaya.utils.LogUtil;
import com.example.himalaya.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

public class SubscriptionFragment  extends BaseFragment implements IRecommendViewCallBack, RecommendListAdapter.onRecommendItemClickListener {
    private static final String TAG = "SubscriptionFragment";
    private View rootView;
    private RecyclerView mrecommendRv;
    private RecommendListAdapter mrecommendListAdapter;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUiLoader;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, ViewGroup container) {
        mUiLoader = new UILoader(getContext()){
            @Override
            protected View getmSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater,container);
            }

//            @Override
//            protected View getmSuccessView(ViewGroup container) {
//                return createSuccessView(layoutInflater,container);
//            }
        };

        //获取到逻辑层的对象
        mRecommendPresenter = RecommendPresenter.getInstance();
        //设置通知接口的注册
        mRecommendPresenter.registerViewCallBack(this);
        //获取推荐列表
        mRecommendPresenter.getRecommendList();

        //与父类解绑,否则会崩溃
        if (mUiLoader.getParent() instanceof ViewGroup){
            ((ViewGroup)mUiLoader.getParent()).removeView(mUiLoader);
        }

        //返回view并显示
        return mUiLoader;
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        //view加载完成
        rootView = layoutInflater.inflate(R.layout.fragment_subscription,container,false);
        //获取推荐列表
        mrecommendRv = rootView.findViewById(R.id.sub_list);
        //设置布局管理器获取布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        //设置垂直方向
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mrecommendRv.setLayoutManager(linearLayoutManager);
        //recycleView 设置上下边距
        mrecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                //数值单位默认是px，要转换为dp
                //UIUtil 工具类 ，功能：dp-->px,px-->dp
                outRect.top = UIUtil.dip2px(view.getContext(),5);
                outRect.bottom = UIUtil.dip2px(view.getContext(),5);
                outRect.left = UIUtil.dip2px(view.getContext(),5);
                outRect.right = UIUtil.dip2px(view.getContext(),5);
            }
        });
        //设置适配器
        mrecommendListAdapter = new RecommendListAdapter();
        mrecommendRv.setAdapter(mrecommendListAdapter);
        mrecommendListAdapter.setOnRecommendItemClickListener(this);
        //返回view并显示
        return rootView;
    }

    @Override
    public void onRecommendListLoaded(List<Album> result) {
        LogUtil.d(TAG,"onRecommendListLoaded --> ");
        //当我们获取推荐内容成功之后，调用这个方法
        //数据回来以后，更新UI，将数据设置给适配器，并且更新UI
        mrecommendListAdapter.setData(result);
        mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onNetworkError() {
        mUiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onEmpty() {
        mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        LogUtil.d(TAG,"onLoading --> ");
        mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消接口引用,避免内存泄漏
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unRegisterViewCallBack(this);
        }

    }

    @Override
    public void onItemClick(Integer tag, Album album) {
        //item被点击了,跳转到详情页面
        AlbumDetailPresenter.getInstance().setTargetAlbum(album);
        Intent intent = new Intent(getContext(), DetailActivity.class);
        startActivity(intent);

    }
}